﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace MgSoft.Import.Excel.Model
{
    /// <summary>
    /// 
    /// </summary>
    public class FileExcelTaskTypeInfo : ExcelTaskTypeInfo, IProcessInfo
    {
        /// <summary>
        /// 锁对象
        /// </summary>
        private object lockObj = new object();
        /// <summary>
        /// 行号
        /// </summary>
        private int rowNumber;
        /// <summary>
        /// 总行数
        /// </summary>
        private int totalRowCount;
        public Guid Id { get; private set; } = Guid.NewGuid();

        /// <summary>
        /// 文件路径
        /// </summary>
        public string FilePath { get; set; }

        public FileExcelTaskTypeInfo()
        {
        }
        public FileExcelTaskTypeInfo(ExcelTaskTypeInfo excelTaskTypeInfo)
        {
            TaskManagerName = excelTaskTypeInfo.TaskManagerName;
            TaskManagerTitle = excelTaskTypeInfo.TaskManagerTitle;
            TaskManagerDescribe = excelTaskTypeInfo.TaskManagerDescribe;
            ComponentName = excelTaskTypeInfo.ComponentName;
            ComponentTitle = excelTaskTypeInfo.ComponentTitle;
            ComponentDescribe = excelTaskTypeInfo.ComponentDescribe;
        }


        public int GetSyncRowNumber()
        {
            return rowNumber;
        }

        public int GetTotalRowCount()
        {
            return totalRowCount;
        }

        public double GetPercent()
        {
            return (double)rowNumber / (double)totalRowCount;
        }

        void IProcessInfo.SetProcessRow(int rowStep)
        {
            lock (lockObj)
            {
                if (rowStep <= 0)
                    throw new MgException("rowSet必须大于0");
                if (totalRowCount == 0)
                    throw new MgException("需要调用SetProcessTotalRow初始化");
                rowNumber += rowStep;
            }
        }

        void IProcessInfo.SetProcessTotalRow(int totleRow)
        {
            lock (lockObj)
            {
                rowNumber = 0;
                totalRowCount = totleRow;
            }
        }

        public void SetFinsh()
        {
            lock (lockObj)
            {
                rowNumber = totalRowCount;
            }
        }
    }
}
